//
//  RegisterViewController.m
//  NiuBanJinGold
//
//  Created by Billy on 15/9/24.
//  Copyright (c) 2015年 zzjr. All rights reserved.
//

#import "RegisterViewController.h"
#import "AgreementViewController.h"
#import "CustomTabBarViewController.h"
#import "User.h"
#import "LoginViewController.h"

@interface RegisterViewController ()

@property (nonatomic, strong) UIView *view_sms ;// SMSView，包括接下来的二个子属性
@property (nonatomic, strong) UILabel *label_promtSMS; //发SMS语言提示
@property (nonatomic, strong) UILabel *label_promtCellNumber; //发SMS的号码

@property (nonatomic, strong) UIView *view_authentication ;// 验证码View，包括接下来的三个子属性
@property (nonatomic, strong) UILabel *label_authenticationPromt; //验证码提示文字
@property (nonatomic, strong) BCTextField *textField_authenticationText; //验证码内容
@property (nonatomic, strong) UIButton *button_retrieveSMS; //重新获取

@property (nonatomic, strong) UIButton *button_promptFailedGetAuthenticationCode ;// 获取不到验证码提示

@property (nonatomic, strong) UIView *view_invitation ;// 邀请码View,包括接下来二个子属性
@property (nonatomic, strong) UILabel *label_invitationPromt; //邀请码提示文字
@property (nonatomic, strong) BCTextField *textField_invitationText; //邀请码内容

@property (nonatomic, strong) UIButton *button_next; //下一步
@property (nonatomic, strong) UIButton *button_agreeText;// 同意内容

@property (nonatomic, strong) UIView *view_container ;//以上所有属性的父view

@property (nonatomic, copy) NSString *str_userGesturePassword;
@property (nonatomic, strong)CLLockVC *lockVC;

@property (nonatomic, assign) BOOL isRetry; //重新发送要加上这个参数

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self pageSettingsAndSubviewsCreation];
    // 调用获取验证码接口
    [self getAuthenticationCode];
    [self countDownMethod];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
}

#pragma mark - SubviewsCreationAndPageSettings

- (void) pageSettingsAndSubviewsCreation {
    
    [self.str_type isEqualToString:@"retrievePassword"] ?[self.navigationItem setTitle:@"忘记密码"]:[self.navigationItem setTitle:@"注册"];
    
    [self.view setBackgroundColor:BASECOLOR];
    // add subviews
    [self.view addSubview:self.view_container];
    
    [self.view_container addSubview:self.view_sms];
    [self.view_container addSubview:self.view_authentication];
    
    if (![self.str_type isEqualToString:@"retrievePassword"]) {
        
        [self.view_container addSubview:self.button_promptFailedGetAuthenticationCode];
        [self.view_container addSubview:self.view_invitation];
        [self.view_container addSubview:self.button_agreeText];
        
    }
    
    [self.view_container addSubview:self.button_next];
    
    // SMS view
    
    
    [self.view_sms addSubview:self.label_promtSMS];
    [self.view_sms addSubview:self.label_promtCellNumber];
    
    //验证码view
    [self.view_authentication addSubview:self.label_authenticationPromt];
    [self.view_authentication addSubview:self.textField_authenticationText];
    [self.view_authentication addSubview:self.button_retrieveSMS];
    
    // 邀请码view
    [self.view_invitation addSubview:self.label_invitationPromt];
    [self.view_invitation addSubview:self.textField_invitationText];
    
    
    // 指定frame[如果UI可以指定比例的话，我们可以改成masonry]
}

#pragma mark - button events handling

- (void) buttonActionAtRegisterPage:(UIButton *)button_sender {
    
    switch (button_sender.tag) {
            
        case 20: //获取验证码，重新获取要加一个参数
        {
            [self countDownMethod];
            
            [self getAuthenticationCode];
        }
            break;
        case 21: //注册
        {
            
            if ([self.str_type isEqualToString:@"register"]) {
                
                [self registerVerification];
            } else  if ([self.str_type isEqualToString:@"retrievePassword"]) { //忘记密码
                
                if ([self.textField_authenticationText.text isEqualToString:@""]) {
                    
                    [UsefulUtil showAlertView:@"验证码不能为空!" ];
                    return;
                }
                //点下一步前去验证SMS对不对
                
                [self passwordForgetten];
            }
        }
            break;
        case 30: //客服电话
        {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"4001826268"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
            break;
            
        case 31: //协议内容
        {
            
            WebViewController *webVC = [[WebViewController alloc]init];
            webVC.index = 10;
            [self.navigationController pushViewController:webVC animated:YES];
        }
        default:
            break;
    }
    
}

// 倒计时
- (void) countDownMethod {
    
    __block int timeout = 60; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置 特别注明：UI的改变一定要在主线程中进行
                [self.button_retrieveSMS setTitle:@"重新获取" forState:UIControlStateNormal];
                self.button_retrieveSMS.userInteractionEnabled = YES;
                
            });
        }else{
            //            int minutes = timeout / 60;
            //            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
            NSString *strTime2 = [NSString stringWithFormat:@"%.d", timeout];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [strTime integerValue] >= 10? [self.button_retrieveSMS setTitle:[NSString stringWithFormat:@"重发(%@)秒",strTime] forState:UIControlStateNormal] : [self.button_retrieveSMS setTitle:[NSString stringWithFormat:@"重发(%@)秒",strTime2] forState:UIControlStateNormal];
                
                self.button_retrieveSMS.userInteractionEnabled = NO;
                
            });
            timeout--;
        }
    });
    
    dispatch_resume(_timer);
}

- (void) getAuthenticationCode { //得到验证码的网络访问方法
    
    NSDictionary *dic_params = nil;
    
    if (self.isRetry) { //如果重试，要加上这个参数
        
        if ([self.str_type isEqualToString:@"register"]) {
            
            dic_params = @{@"mobile":self.str_cellNumber,@"type":@"0",@"isRetry":@"1"};
            
        } else if ([self.str_type isEqualToString:@"retrievePassword"]) {
            
            dic_params = @{@"mobile":self.str_cellNumber,@"type":@"1",@"isRetry":@"1"};
        }
        
    }else  {
        
        if ([self.str_type isEqualToString:@"register"]) {
            
            dic_params = @{@"mobile":self.str_cellNumber,@"type":@"0"};
            
        } else if ([self.str_type isEqualToString:@"retrievePassword"]) {
            
            dic_params = @{@"mobile":self.str_cellNumber,@"type":@"1"};
        }
    }
    
    [[NetworkUtil sharedNetworkUtil]  zzjrGetSMSAuthenticationCodeByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        if (!error) {  //没有网络错误
            
            // 开始解析数据
            NSDictionary *dic = parsedData;
            
            if([dic[@"status"] intValue] == 0) {
                
                NSString *result = [NSString stringWithFormat:@"%@",dic[@"result"][@"authenticationCode"]];
//                [UsefulUtil showAlertView:[NSString stringWithFormat:@" 验证码是:%@",result]];
                
                NSLog(@"%@",[NSString stringWithFormat:@" 验证码是:%@",result]);
            } else {// 不是网络出错, 其它错误
                
                [UsefulUtil showAlertView:dic[@"msg"]];
                [self.button_next setUserInteractionEnabled:YES];
            }
        }else {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
            [self.button_next setUserInteractionEnabled:YES];
        }
    }];
}

//忘记密码

- (void) passwordForgetten {
    
    // 先验证验证码正确性 , 正确==>手势密码设置, OR 提示
    
    NSDictionary *dic_params = @{@"mobile":self.str_cellNumber,@"authenticationCode":self.textField_authenticationText.text,@"invitationCode":self.textField_invitationText.text,@"type":@"1"};
    
    [[NetworkUtil sharedNetworkUtil] zzjrSMSAuthenticationCodeComparisonByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if (!error) {  //没有网络错误
            
            // 开始解析数据
            NSDictionary *dic = parsedData;
            
            if([dic[@"status"] intValue] == 0) {
                
                NSString *result = [NSString stringWithFormat:@"%@",dic[@"result"][@"resultCode"]];
                
                if ([result isEqualToString:@"0"]) { //验证码正确
                    
                    //验证码正确的情况下，进行手势设置
                    
                    //开始找回流程
                    [self gesturePassordRest];
                }
            } else {// 不是网络出错, 其它错误
                
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                
                [UsefulUtil showAlertView:dic[@"msg"]];
                [self.button_next setUserInteractionEnabled:YES];
            }
        }else {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
            [self.button_next setUserInteractionEnabled:YES];
        }
    }];
}

- (void) gesturePassordRest { //SMS通过后去验证
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [CLLockVC showSettingLockVCInVC:self successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            
            NSDictionary *dic_params = @{@"mobile":self.str_cellNumber,@"newGesturePassword":pwd,@"channel":@"appstroe"};
            
            [[NetworkUtil sharedNetworkUtil] zzjrUserForgetGesturePasswordByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
                
                if (!error) {  //没有网络错误
                    
                    // 开始解析数据
                    NSDictionary *dic = parsedData;
                    
                    if([dic[@"status"] intValue] == 0) {
                        
                        // 保存为用户对象
                        User *user = [[User alloc] initWithAttributes:dic[@"result"]];
                        [[UserManager sharedUserManager] setUser:user];
                        [[UserManager sharedUserManager] saveCurrentUser:user];
                        
                        [lockVC dismiss:0];
                        CURRENT_WINDOW.rootViewController = [CustomTabBarViewController new];
                    } else {// 不是网络出错, 其它错误
                        
                        [UsefulUtil showAlertView:dic[@"msg"]];
                    }
                }else {
                    
                    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                    [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
                    [self.button_next setUserInteractionEnabled:YES];
                }
            }];
            
        }];
    });
}

//注册验证

- (void) registerVerification {
    
    if ([self.textField_authenticationText.text isEqualToString:@""]) {
        
        [UsefulUtil showAlertView:@"请输入验证码!"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; //进度条
    
    // 先验证验证码正确性 , 正确==>手势密码设置, OR 提示
    
    NSDictionary *dic_params = @{@"mobile":self.str_cellNumber,@"authenticationCode":self.textField_authenticationText.text,@"invitationCode":self.textField_invitationText.text,@"type":@"0"};
    
    [[NetworkUtil sharedNetworkUtil] zzjrSMSAuthenticationCodeComparisonByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if (!error) {  //没有网络错误
            
            // 开始解析数据
            NSDictionary *dic = parsedData;
            
            if([dic[@"status"] intValue] == 0) {
                
                NSString *result = [NSString stringWithFormat:@"%@",dic[@"result"][@"resultCode"]];
                
                if ([result isEqualToString:@"0"]) { //验证码正确
                    
                    //验证码正确的情况下，进行手势设置
                    
                    [self gesturePasswordSettingForRegister];
                }
            } else {// 不是网络出错, 其它错误
                
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                
                [UsefulUtil showAlertView:dic[@"msg"]];
                [self.button_next setUserInteractionEnabled:YES];
            }
        }else {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
            [self.button_next setUserInteractionEnabled:YES];
        }
    }];
}

- (void) gesturePasswordSettingForRegister {
    
    dispatch_async(dispatch_get_main_queue(), ^(void){
        
        [CLLockVC showSettingLockVCInVC:self successBlock:^(CLLockVC *lockVC, NSString *pwd) {

            self.str_userGesturePassword = pwd;
            self.lockVC = lockVC;
            
            [self exactlyRegisterProcess];
        }];
    });
}

- (void) exactlyRegisterProcess { //真正开始注册流程
    
    // 设置成功的情况下调用注册功能
    [MBProgressHUD showHUDAddedTo:self.lockVC.view animated:YES]; //进度条
    
    NSDictionary *dic_params = @{@"mobile":self.str_cellNumber,@"gesturePassword":self.str_userGesturePassword,@"udidStr":[UsefulUtil getUDIDOfCurrentDevice],@"channel":@"appstore",@"platform":@"4"};
    
    [[NetworkUtil sharedNetworkUtil] zzjrUserRegisterByParmeteres:dic_params  transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.lockVC.view animated:YES];
        
        if (!error) {  //没有网络错误
            
            // 开始解析数据
            NSDictionary *dic = parsedData;
            
            if([dic[@"status"] intValue] == 0) {
                
                // 保存为用户对象
             
                User *user = [[User alloc] initWithAttributes:dic[@"result"]];
                [[UserManager sharedUserManager] setUser:user];
                [[UserManager sharedUserManager] saveCurrentUser:user];
                
                [self.lockVC dismiss:0];
                
                
                CURRENT_WINDOW.rootViewController = [CustomTabBarViewController new];
            } else {// 不是网络出错, 其它错误
                
                [MBProgressHUD hideAllHUDsForView:self.lockVC.view animated:YES];
                
                
//                // BUG 16要求删除重试功能，代替方案为直接显示服务器返回的错误
//                
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dic[@"msg"] message:nil delegate:self cancelButtonTitle:@"重试" otherButtonTitles:nil, nil];
//                
//                [alert show];
                
                [UsefulUtil showAlertView:dic[@"msg"]];
            }
        }else {
            
            [MBProgressHUD hideAllHUDsForView:self.lockVC.view animated:YES];
            [UsefulUtil showAlertView:@"遇到网络问题，请稍候重试!"];
        }
    }];

}
//  // BUG 16要求删除重试功能，代替方案为直接显示服务器返回的错误
//
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// 
//    switch (buttonIndex) {
//        case 0: //重试上传用户数据
//            
//        {
//            [self exactlyRegisterProcess]; //重试
//        }
//            break;
//            
//        default:
//            break;
//    }
//}

#pragma mark - setter methods

- (UIView *)view_container {
    
    if (!_view_container) {
        
        _view_container = [self viewCreationWithBackgroundColor:BASECOLOR];
        [_view_container setFrame:CGRectMake(0, 0, SCREEN_WIDTH, (170.0 + 100 + 65 +100 +62 + 90 + 20+ 40) *SCALE_RATE_HEIGHT/2)];
        //        [_view_container setBackgroundColor:[UIColor orangeColor]]; // debug用
    }
    
    return  _view_container;
}

- (UIView *)view_sms {
    
    if (!_view_sms) {
        
        _view_sms = [self viewCreationWithBackgroundColor:BASECOLOR];
        [_view_sms setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170.0 * SCALE_RATE_HEIGHT/2)];
    }
    
    return  _view_sms;
}

- (UILabel *)label_promtSMS {
    
    if (!_label_promtSMS) {
        
        _label_promtSMS = [self labelCreationWithTextColor:[UsefulUtil colorWithHexString:@"#9d9d9d"] andFont:[UIFont systemFontOfSize:15] andText:@"我们已将验证码 (免费) 发送至"];
        [_label_promtSMS setFrame:CGRectMake(0, 38.0 * SCALE_RATE_HEIGHT/2, SCREEN_WIDTH, 30.0 * SCALE_RATE_HEIGHT/2)];
    }
    
    return _label_promtSMS;
}

- (UILabel *)label_promtCellNumber {
    
    if (!_label_promtCellNumber) {
        
        _label_promtCellNumber  = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.label_promtSMS.frame)+ 30.0 * SCALE_RATE_HEIGHT/2, SCREEN_WIDTH, 30.0 * SCALE_RATE_HEIGHT/2)];
        [_label_promtCellNumber setTextAlignment:NSTextAlignmentCenter];
        [_label_promtCellNumber setText:self.str_cellNumber];
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_label_promtCellNumber.text];
        [attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Light" size:20.0*SCALE_RATE_HEIGHT] range:NSMakeRange(0,_label_promtCellNumber.text.length)];
        
        [_label_promtCellNumber setAttributedText:attrStr];
    }
    
    return _label_promtCellNumber;
}

- (UIView *)view_authentication {
    
    if (!_view_authentication) {
        
        _view_authentication = [self viewCreationWithBackgroundColor:[UIColor whiteColor]];
        [_view_authentication setFrame:CGRectMake(0, CGRectGetMaxY(self.view_sms.frame), SCREEN_WIDTH, 100.0 * SCALE_RATE_HEIGHT/2)];
    }
    
    return _view_authentication;
}

- (UILabel *)label_authenticationPromt {
    
    if (!_label_authenticationPromt) {
        
        _label_authenticationPromt = [self labelCreationWithTextColor:[UsefulUtil colorWithHexString:@"#434343"] andFont:[UIFont systemFontOfSize:15] andText:@"验证码"];
        [_label_authenticationPromt setTextAlignment:NSTextAlignmentCenter];
        
        [_label_authenticationPromt setFrame:CGRectMake(30.0*SCALE_RATE_WIDTH/2, 0, 84.0*SCALE_RATE_WIDTH/2, 100.0*SCALE_RATE_HEIGHT/2)];
    }
    
    return _label_authenticationPromt;
}

- (BCTextField *)textField_authenticationText {
    
    if (!_textField_authenticationText) {
        
        [_textField_authenticationText setBackgroundColor:[UIColor redColor]];
        _textField_authenticationText = [self textFieldCreationWithPlaceholder:@"请输入短信验证码"];
        [_textField_authenticationText setAdjustsFontSizeToFitWidth:YES];
        [_textField_authenticationText setFrame:CGRectMake(CGRectGetMaxX(self.label_authenticationPromt.frame) +40.0*SCALE_RATE_WIDTH/2, 0, 380.0*SCALE_RATE_WIDTH/2, 100.0*SCALE_RATE_HEIGHT/2)];
        [_textField_authenticationText setRegx:@"^[0-9]{1,4}$"];
        [_textField_authenticationText setTag:23];
    }
    
    return _textField_authenticationText;
}

- (UIButton *)button_retrieveSMS {
    
    if (!_button_retrieveSMS) {
        
        _button_retrieveSMS = [self buttonCreationWithTitle:@"重新获取" andTextColor:[UsefulUtil colorWithHexString:@"#4fa6fd"] andTextFont:[UIFont systemFontOfSize:15]];
        
        [_button_retrieveSMS setFrame:CGRectMake(CGRectGetMaxX(self.textField_authenticationText.frame), 0, SCREEN_WIDTH - CGRectGetMaxX(self.textField_authenticationText.frame) - 30.0*SCALE_RATE_WIDTH/2, 100.0*SCALE_RATE_HEIGHT/2)];
        [_button_retrieveSMS setTag:20];
        
        [_button_retrieveSMS addTarget:self action:@selector(buttonActionAtRegisterPage:) forControlEvents:UIControlEventTouchUpInside];
        
        self.isRetry = YES;
    }
    
    return  _button_retrieveSMS;
}

- (UIButton *)button_promptFailedGetAuthenticationCode {
    
    if (!_button_promptFailedGetAuthenticationCode) {
        
        _button_promptFailedGetAuthenticationCode  = [self buttonCreationWithTitle:@"获取不到验证码? 联系客服" andTextColor:[UsefulUtil colorWithHexString:@"#4fa6fd"] andTextFont:[UIFont systemFontOfSize:11.0]];
        [_button_promptFailedGetAuthenticationCode setFrame:CGRectMake(30.0 * SCALE_RATE_WIDTH /2, CGRectGetMaxY(self.view_authentication.frame), SCREEN_WIDTH, 62.0 * SCALE_RATE_HEIGHT/2)];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_button_promptFailedGetAuthenticationCode.currentTitle];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UsefulUtil colorWithHexString:@"#4fa6fd"] range:NSMakeRange(0, _button_promptFailedGetAuthenticationCode.currentTitle.length)];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UsefulUtil colorWithHexString:@"#5f5f61"] range:NSMakeRange(0, 9)];
        [_button_promptFailedGetAuthenticationCode setAttributedTitle:attrStr forState:UIControlStateNormal];
        
        [_button_promptFailedGetAuthenticationCode setTag:30];
        //        [_button_promptFailedGetAuthenticationCode setTitleEdgeInsets:UIEdgeInsetsMake(0, -30.0 * SCALE_RATE_WIDTH /2, 0, 0)];
        [_button_promptFailedGetAuthenticationCode setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    }
    
    return _button_promptFailedGetAuthenticationCode;
}

- (UIView *)view_invitation {
    
    if (!_view_invitation) {
        
        _view_invitation = [self viewCreationWithBackgroundColor:[UIColor whiteColor]];
        [_view_invitation setFrame:CGRectMake(0, CGRectGetMaxY(self.button_promptFailedGetAuthenticationCode.frame), SCREEN_WIDTH, 100.0 * SCALE_RATE_HEIGHT /2)];
    }
    
    return _view_invitation;
}

- (UILabel *)label_invitationPromt {
    
    if (!_label_invitationPromt) {
        
        _label_invitationPromt = [self labelCreationWithTextColor:[UsefulUtil colorWithHexString:@"#434343"] andFont:[UIFont systemFontOfSize:15] andText:@"邀请码"];
        [_label_invitationPromt setTextAlignment:NSTextAlignmentCenter];
        
        [_label_invitationPromt setFrame:CGRectMake(30.0*SCALE_RATE_WIDTH/2, 0, 84.0*SCALE_RATE_WIDTH/2, 100.0*SCALE_RATE_HEIGHT/2)];
    }
    
    return _label_invitationPromt;
}

- (BCTextField *)textField_invitationText {
    
    if (!_textField_invitationText) {
        
        [_textField_invitationText setBackgroundColor:[UIColor redColor]];
        _textField_invitationText = [self textFieldCreationWithPlaceholder:@"邀请人的邀请码或手机号(选填)"];
        [_textField_invitationText setAdjustsFontSizeToFitWidth:YES];
        [_textField_invitationText setFrame:CGRectMake(CGRectGetMaxX(self.label_invitationPromt.frame) +40.0*SCALE_RATE_WIDTH/2, 0, SCREEN_WIDTH -CGRectGetMaxX(self.label_invitationPromt.frame) , 100.0*SCALE_RATE_HEIGHT/2)];
        
        [_textField_invitationText setKeyboardType:UIKeyboardTypeDefault];
        [_textField_invitationText setRegx:@"^[a-zA-Z0-9]{0,11}$"];
        [_textField_invitationText setTag:22];
    }
    
    return  _textField_invitationText;
}


- (UIButton *)button_next {
    
    if (!_button_next) {
        
        _button_next = [self buttonCreationWithTitle:@"下一步" andTextColor:[UsefulUtil colorWithHexString:@"#ffffff"] andTextFont:[UIFont systemFontOfSize:17.0]];
        [_button_next.titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
        UIView *view_judge = [self.str_type isEqualToString:@"register"]? self.view_invitation: self.view_authentication;
        
        [_button_next setFrame:CGRectMake(30*SCALE_RATE_WIDTH, CGRectGetMaxY(view_judge.frame) + 62.0 * SCALE_RATE_HEIGHT/2, SCREEN_WIDTH - 60.0 * SCALE_RATE_WIDTH, 90.0*SCALE_RATE_HEIGHT/2)];
        
        [_button_next setBackgroundColor:[UsefulUtil colorWithHexString:@"#fb4747"]];
        [_button_next setTag:21];
        [_button_next addTarget:self action:@selector(buttonActionAtRegisterPage:) forControlEvents:UIControlEventTouchUpInside];
        
        [_button_next.layer setCornerRadius:8.0];
    }
    
    return  _button_next;
    
}

- (UIButton *)button_agreeText {
    
    if (!_button_agreeText) {
        
        _button_agreeText = [self buttonCreationWithTitle:@"点击下一步按钮即表示您同意 《牛板金服务协议》" andTextColor:nil andTextFont:[UIFont systemFontOfSize:11]];
        [_button_agreeText setFrame:CGRectMake(0, CGRectGetMaxY(self.button_next.frame)+20.0*SCALE_RATE_HEIGHT/2, SCREEN_WIDTH, 20.0)];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_button_agreeText.currentTitle];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UsefulUtil colorWithHexString:@"#4fa6fd"] range:NSMakeRange(0, _button_agreeText.currentTitle.length)];
        
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UsefulUtil colorWithHexString:@"#5f5f61"] range:NSMakeRange(0, 13)];
        
        [_button_agreeText setTag:31];
        [_button_agreeText setAttributedTitle:attrStr forState:UIControlStateNormal];
    }
    
    return  _button_agreeText;
}

- (UILabel *) labelCreationWithTextColor:(UIColor *) color andFont:(UIFont *) font andText:(NSString *) text{
    
    UILabel *label_tmp = [[UILabel alloc] init];
    [label_tmp setTextAlignment:NSTextAlignmentCenter];
    [label_tmp setTextColor:color];
    [label_tmp setFont:font];
    [label_tmp setText:text];
    [label_tmp setAdjustsFontSizeToFitWidth:YES];
    return  label_tmp;
}

- (UIView *) viewCreationWithBackgroundColor: (UIColor *) color {
    
    UIView *view_tmp  = [[UIView alloc] init];
    [view_tmp setBackgroundColor:color];
    
    return view_tmp;
}

- (BCTextField *) textFieldCreationWithPlaceholder:(NSString *) placeHolder {
    
    BCTextField *textFiled_tmp = [[BCTextField alloc] init];
    
    [textFiled_tmp setPlaceholder:placeHolder];
    [textFiled_tmp setClearButtonMode:UITextFieldViewModeWhileEditing];
    [textFiled_tmp setKeyboardType:UIKeyboardTypeNumberPad];
    
    return textFiled_tmp;
}

- (UIButton *) buttonCreationWithTitle:(NSString *) text andTextColor:(UIColor *) color andTextFont:(UIFont *) font {
    
    UIButton *button_tmp = [[UIButton alloc] init];
    [button_tmp setTitle:text forState:UIControlStateNormal];
    [button_tmp setTitleColor:color forState:UIControlStateNormal];
    [button_tmp.titleLabel setFont:font];
    [button_tmp addTarget:self action:@selector(buttonActionAtRegisterPage:) forControlEvents:UIControlEventTouchUpInside];
    return button_tmp;
}

// here no need to implement such gesture method
- (void) swiperGestureHandle:(UISwipeGestureRecognizer *) swipe {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    self.str_userGesturePassword = nil;
    self.lockVC = nil;
}

- (void) backToPreviousPage {
    
    if ([self.str_type isEqualToString:@"retrievePassword"]) {
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
        
      CURRENT_WINDOW.rootViewController = nav;
    }else {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
